﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.Text;

namespace Runda.CMS.Portal.SalesManage
{
    public partial class AccountClose : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "customername";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                
                BindGrid();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblImport = e.Item.FindControl("lblImport") as Label;
                Label lblAdjust = e.Item.FindControl("lblAdjust") as Label;
                Label lblClose = e.Item.FindControl("lblClose") as Label;
                Label lblisLock = e.Item.FindControl("lblisLock") as Label;
                Label lblisAdjust = e.Item.FindControl("lblisAdjust") as Label;
                Label lblisClose = e.Item.FindControl("lblisClose") as Label;
                Label lblSaleAdjust = e.Item.FindControl("lblSaleAdjust") as Label;
                Label lblisSaleAdjust = e.Item.FindControl("lblisSaleAdjust") as Label;

                LinkButton lnkbtnCancelImport = e.Item.FindControl("lnkbtnCancelImport") as LinkButton;
                LinkButton lnkbtnCancelAdjust = e.Item.FindControl("lnkbtnCancelAdjust") as LinkButton;
                LinkButton lnkbtnCancelClose = e.Item.FindControl("lnkbtnCancelClose") as LinkButton;
                LinkButton lnkbtnCancelSaleAdjust = e.Item.FindControl("lnkbtnCancelSaleAdjust") as LinkButton;

                if (lblisLock.Text == "1") { lblImport.Visible = true; lnkbtnCancelImport.Visible = true; }
                if (lblisAdjust.Text == "1") { lblAdjust.Visible = true; lnkbtnCancelAdjust.Visible = true; }
                if (lblisClose.Text == "1") { lblClose.Visible = true; lnkbtnCancelClose.Visible = true; }
                if (lblisSaleAdjust.Text == "1") { lblSaleAdjust.Visible = true; lnkbtnCancelSaleAdjust.Visible = true; }

                //if( !base.CheckUserHasFuction(1, "Func10013") )
                //{
                //    lnkbtnCancelImport.Visible = false;
                //    lnkbtnCancelAdjust.Visible = false;
                //    lnkbtnCancelClose.Visible = false;
                //}
            }
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "cancelimport")
            {

                BudgetBll bll = new BudgetBll();

                bll.cancelDataImportStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            }
            if (e.CommandName.ToLower() == "canceladjust")
            {

                BudgetBll bll = new BudgetBll();

                bll.cancelDataAdjustStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            }
            if (e.CommandName.ToLower() == "cancelsaleadjust")
            {

                BudgetBll bll = new BudgetBll();

                bll.cancelSaleDataAdjustStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            }
            if (e.CommandName.ToLower() == "cancelclose")
            {

                BudgetBll bll = new BudgetBll();

                bll.cancelDataCloseStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            }
            BindGrid();
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        } 

        private void BindGrid()
        {
            string sb = getWhereClause();

            DataTable dt = new BillBll().getAccountStatusList(sb);

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid();
        }

        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            
            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and iYear = ").Append(ddlBudgetYear.SelectedValue);
            }

            if (ddlCompany.SelectedValue != null)
            {
                sb.Append(" and company = '").Append(ddlCompany.SelectedValue).Append("'");
            }
            
            return sb.ToString();
        }

        
    }
}
